import Vue from "vue";
import Router from "vue-router";
import store from "@/store";
import doc from "./doc";

const routerPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => {
    console.log(error);
  });
};
Vue.use(Router);
const router = new Router({
  routes: [
    {
      path: "/",
      name: "home",
      component: () => import("@/Home.vue"),
      redirect: "/Login",
      meta: {
        keepAlive: true
      },
      children: [
        ...doc,
        {
          path: "/option",
          name: "option",
          component: () => import("@/views/option/option.vue"),
          meta: {
            keepAlive: true
          }
        },
        {
          path: "/index",
          name: "index",
          component: () => import("@/views/Index.vue"),
          meta: {
            keepAlive: true
          }
        },
        // 客户备案
        {
          path: "/record",
          name: "record",
          component: () => import("@/views/record/record.vue"),
          meta: {
            keepAlive: true
          }
        },

        // 我的页面
        {
          path: "/user",
          name: "user",
          component: () => import("@/views/user/index.vue"),
          meta: {
            keepAlive: true
          }
        },
        // 九宫格首页
        {
          path: "/grid",
          name: "grid",
          component: () => import("@/views/grid/grid.vue"),
          meta: {
            keepAlive: true
          }
        },
        // 客户页面
        {
          path: "/formal",
          name: "formal",
          component: () => import("@/views/formal/formal.vue"),
          meta: {
            keepAlive: true
          }
        },

        {
          path: "/Communication",
          name: "Communication",
          component: () => import("@/views/Cust_Communication/List.vue"), //沟通记录
          meta: {
            keepAlive: true
          }
        },
        // 交易
        {
          path: "/transaction",
          name: "transaction",
          component: () => import("@/views/Transaction/List.vue"),
          meta: {
            keepAlive: true
          }
        },
        // 报表
        {
          path: "/excel",
          name: "excel",
          component: () => import("@/views/excel/excel.vue"),
          meta: {
            keepAlive: true
          }
        },
        // 工单页面
        {
          path: "/workOrder",
          name: "workOrder",
          component: () => import("@/views/workOrder/workOrder.vue"),
          meta: {
            keepAlive: true
          }
        },
        // 工单管理页
        {
          path: "/workOrder/workOrderManage",
          name: "workOrderManage",
          component: () =>
            import("@/views/workOrderManage/workOrderManage.vue"),
          meta: {
            keepAlive: true
          }
        },
        // 我的工单页
        {
          path: "/myWorkOrder",
          name: "myWorkOrder",
          component: () => import("@/views/myWorkOrder/myWorkOrder.vue"),
          meta: {
            keepAlive: true
          }
        },
        // 样品页面
        {
          path: "/service",
          name: "service",
          component: () => import("@/views/service/sample.vue"),
          meta: {
            keepAlive: true
          }
          //   children: [
          //   {
          //     path: '/seredit',
          //     name: 'seredit',
          //     component: () => import('@/views/service/Edit.vue'),
          //     meta: {
          //       keepAlive: true
          //     }
          //   },
          // ]
        }
      ]
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/Login.vue"),
      meta: {
        anonymous: true
      }
    },
    {
      path: "/404",
      name: "404",
      component: () => import("@/views/redirect/404.vue"),
      meta: {}
    },
    {
      path: "/transaction/Formdetails",
      name: "transactionFormdetails",
      component: () => import("@/views/Transaction/Formdetails.vue"),
      meta: {
        keepAlive: false
      }
    },
    {
      path: "/formal/Viewdetails",
      name: "Viewdetails",
      component: () => import("@/views/formal/Viewdetails.vue"),
      meta: {
        keepAlive: false
      }
    },
    // 和上面的order路由重复了
    // {
    //   path: "/formal/Viewdetails/order",
    //   name: "order",
    //   component: () => import("@/views/formal/order.vue"),
    //   meta: {
    //     keepAlive: false
    //   }
    // },

    {
      path: "/stat",
      name: "stat",
      component: () => import("@/views/stat/index.vue"),
      meta: {}
    },
    // 样品新增
    {
      path: "/service/Edit",
      name: "Edit",
      component: () => import("@/views/service/Edit.vue"),
      meta: {
        keepAlive: false
      }
    },
    // 联系人编辑
    {
      path: "/record/Edit",
      name: "Edit",
      component: () => import("@/views/record/EditRecord.vue"),
      meta: {
        keepAlive: false
      }
    },
    // 联系人新增
    {
      path: "/record/Add",
      name: "Add",
      component: () => import("@/views/record/AddRecord.vue"),
      meta: {
        keepAlive: false
      }
    },
    // 客户备案
    {
      path: "/formal/Add",
      name: "Add",
      component: () => import("@/views/formal/AddFormal.vue"),
      meta: {
        keepAlive: false
      }
    },
    {
      path: "/service/work",
      name: "work",
      component: () => import("@/views/service/work.vue"),
      meta: {
        keepAlive: false
      }
    },
    {
      path: "/service/myWork",
      name: "myWork",
      component: () => import("@/views/service/myWork.vue"),
      meta: {
        keepAlive: false
      }
    },
    // 沟通记录页
    {
      path: "/Communication/addForm",
      name: "AddForm",
      component: () => import("@/views/Cust_Communication/addForm.vue"),
      meta: {
        keepAlive: false
      }
    },
    // 新增沟通记录页
    {
      path: "/Communication/EditForm",
      name: "EditForm",
      component: () => import("@/views/Cust_Communication/EditForm.vue"),
      meta: {
        keepAlive: false
      }
    },
    {
      path: "/potential",
      name: "potential",
      component: () => import("@/views/potential/potential.vue"),
      meta: {
        keepAlive: true
      }
    },
    {
      path: "/price",
      name: "price",
      component: () => import("@/views/Price/price.vue"),
      meta: {
        keepAlive: true
      }
    },
    {
      path: "/myprice",
      name: "myprice",
      component: () => import("@/views/MyPrice/myprice.vue"),
      meta: {
        keepAlive: true
      }
    },
    {
      path: "/community",
      name: "community",
      component: () => import("@/views/Community/community.vue"),
      meta: {
        keepAlive: true
      }
    },

    {
      path: "/sample",
      name: "sample",
      component: () => import("@/views/Sample/sample.vue"),
      meta: {
        keepAlive: true
      }
    },
    {
      path: "/delivery",
      name: "delivery",
      component: () => import("@/views/Delivery/delivery.vue"),
      meta: {
        keepAlive: true
      }
    },
    {
      path: "/Information",
      name: "Information",
      component: () => import("@/views/Information/Information.vue"),
      meta: {
        keepAlive: true
      }
    },
    //订单
    {
      path: "/order",
      name: "order",
      component: () => import("@/views/Order/order.vue"),
      meta: {
        keepAlive: true
      }
    },

    {
      path: "/mysample",
      name: "mysample",
      component: () => import("@/views/MySample/index.vue"),
      meta: {
        keepAlive: true
      }
    },
    {
      path: "/myoption",
      name: "myoption",
      component: () => import("@/views/Myoption/myoption.vue"),
      meta: {
        keepAlive: true
      }
    },
    {
      path: "/product",
      name: "product",
      component: () => import("@/views/product/product.vue"),
      meta: {
        keepAlive: true
      }
    },

    //返料
    {
      path: "/returnMaterial",
      name: "returnMaterial",
      component: () => import("@/views/ReturnMaterial/returnMaterial.vue"),
      meta: {
        keepAlive: true
      }
    },
    {
      path: "/app_appointment",
      name: "app_appointment",
      component: () => import("@/views/page/app_appointment.vue"),
      meta: {}
    },
    {
      path: "/app_transactionAvgPrice",
      name: "app_transactionAvgPrice",
      component: () => import("@/views/page/app_transactionAvgPrice.vue"),
      meta: {}
    },
    {
      path: "/App_Expert",
      name: "App_Expert",
      component: () => import("@/views/page/App_Expert.vue"),
      meta: {}
    },
    {
      path: "/sellOrder",
      name: "SellOrder",
      component: () => import("@/views/page/sellOrder/SellOrder.vue"),
      meta: {}
    },
    {
      path: "/sellOrder/detail",
      name: "SellOrder_detail",
      component: () => import("@/views/page/sellOrder/detail.vue"),
      meta: {}
    },
    {
      path: "/test",
      name: "test",
      component: () => import("@/views/test/Test.vue"),
      meta: {}
    }
  ]
});

router.beforeEach((to, from, next) => {
  if (to.matched.length == 0) return next({ path: "/404" });
  store.dispatch("onLoading", true);

  if (
    (to.hasOwnProperty("meta") && to.meta.anonymous) ||
    store.getters.isLogin()
  ) {
    return next();
  }

  next({ path: "/login", query: { redirect: Math.random() } });
});

router.afterEach((to, from) => {
  store.dispatch("onLoading", false);
});

router.onError(error => {
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  const targetPath = router.history.pending.fullPath;
  console.log(error.message);
  console.log(targetPath);
  if (isChunkLoadFailed) {
    window.location.replace(window.location.href);
  }
});

export default router;
